package servlet;

import service.VoteService;
import service.impl.VoteServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/vote")
public class VoteServlet extends HttpServlet {
    private VoteService voteService =new VoteServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String []techInterest = request.getParameterValues("techInterest");

        try {
            for (String i:techInterest){
                switch (i){
                    case "Java":
                        voteService.changeVote("Java");
                        break;
                    case ".NET":
                        voteService.changeVote(".NET");
                        break;
                    case "Android" :
                        voteService.changeVote("Android");
                        break;
                    case "PHP":
                        voteService.changeVote("PHP");
                        break;
                }
            }
            response.sendRedirect("vote_success.html");
        } catch (IOException e) {
            response.sendRedirect("vote_failed.html");
            throw new RuntimeException(e);
        }
    }
}
